

The bzz handshake protocol is the protocol that is always run after two peers are connected and before any other protocols are established. It communicates information about the  peer's address, network ID and light node capability.

The handshake protocol defines only one stream and three messages:

\begin{definition}[Bzz handshake protocol  messages]\label{def:bzz-messages}

\begin{lstlisting}
// ID: /swarm/handshake/1.0.0/handshake

syntax = "proto3";

package handshake;

message Syn {
    bytes ObservedUnderlay = 1;
}

message Ack {
    BzzAddress Address = 1;
    uint64 NetworkID = 2;
    bool Light = 3;
}

message SynAck {
    Syn Syn = 1;
    Ack Ack = 2;
}

message BzzAddress {
    bytes Underlay = 1;
    bytes Signature = 2;
    bytes Overlay = 3;
}
\end{lstlisting}
\end{definition}

This message sequence is inspired by the TCP three way handshake to ensure message deliverability.

Upon connection, requesting peer constructs a new handshake stream and sends a \lstinline{Syn} message with the remotely observed Underlay address of the peer it is performing the handshake with. After that it waits for \lstinline{SynAck} response message from the responding peer. In  the  \lstinline{SynAck} message responder sends it own \lstinline{Syn} message as well, together with the acknowledgement message which should include it's correctly signed BzzAddress. The received observed address can be used and compared with locally known addresses in order to send the better advertisable address (Underlay) in the acknowledgement. After the requesting peer receives the \lstinline{SynAck} message from the responding peer and validates that the received \lstinline{Ack} information in it is correct, it sends an \lstinline{Ack} message itself as a confirmation to the responding peer. The stream is closed by the responding peer after it receives the \lstinline{Ack} message.

The connection must be terminated if network IDs are do not match or if the exact order of messages is not followed.

The bzz address is verified and overlay, underlay and signature are extracted.
light is a boolean field indicating whether the node is operating as a light (as opposed to full) node.

After the handshake,  each peer should remember the following data about the other:

\begin{itemize}
    \item the overlay address - used in forwarding  (see  \ref{spec:strategy:forwarding}),
    \item the underlay address - used for dialing, passed to the underlay network protocol when the connectivity driver needs to connect to the peer (see \ref{spec:strategy:connection}),
    \item the bzz address signature - needed by the hive protocol to pass information about the node to other peers (see \ref{spec:protocol:hive}),
    \item whether the peer is a light node.
\end{itemize}

% \subsection{Encapsulation of price information \statusred}

